﻿#include<Windows.h>
#include"resource.h"
const char g_szClassName[]="myWindowClass";
HWND g_Result=NULL;
HDC hdc;
const int ID_TIMER=1;
const int temp=2;
const int value=10;
typedef struct tagBall
{
	int x;
	int y;
	int width;
	int height;
	int dx;
	int dy;
}BALLINFO;
BALLINFO ball;
BOOL CALLBACK DlgProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch (msg)
	{
	case WM_INITDIALOG:
		return FALSE;
	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
			EndDialog(hwnd,IDOK);
			break;
		}
		break;
	case WM_CLOSE:
		EndDialog(hwnd,0);
		break;
	default:
		return FALSE;
	}
	return TRUE;
}

void DrawBall(HWND hwnd, HDC hdc)
{
	RECT rcClient;
	GetClientRect(hwnd,&rcClient);
	HPEN hPen,hPenOld;
	PAINTSTRUCT ps;
	COLORREF color;
	HBRUSH hBrush,hOldBrush;
	hBrush=(HBRUSH)CreateSolidBrush(RGB(0,0,0));
	hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);
	Rectangle(hdc, rcClient.left , rcClient.top,rcClient.right,rcClient.bottom);
	color=RGB(255,0,0);
	hPen=CreatePen(PS_SOLID,3,color);
	hPenOld=(HPEN)SelectObject(hdc,hPen);
	hBrush=(HBRUSH)CreateSolidBrush(RGB(255 ,246 ,143));
	hOldBrush=(HBRUSH)SelectObject(hdc,hBrush);
	
	Ellipse(hdc,ball.x,ball.y,ball.width,ball.height);

	SelectObject(hdc,hOldBrush);
	DeleteObject(hBrush);
	SelectObject(hdc,hPenOld);
	DeleteObject(hPen);
	InvalidateRect(hwnd,&rcClient, 0); 
}
void DrawRectangle(HWND hwnd,HDC hdc,int x,int width)
{
	RECT rcClient;
	GetClientRect(hwnd,&rcClient);
	HPEN hPen,hPenOld;
	PAINTSTRUCT ps;
	COLORREF color;
	HBRUSH hBrush,hBrushOld;
	color=RGB(255,0,0);
	hPen=CreatePen(PS_SOLID,3,color);
	hPenOld=(HPEN)SelectObject(hdc,hPen);
	hBrush=(HBRUSH)CreateSolidBrush(RGB(255,0,0));
	hBrushOld=(HBRUSH)SelectObject(hdc,hBrush);
			
	Rectangle(hdc,x,rcClient.bottom-10,width,rcClient.bottom-30);
		
	SelectObject(hdc,hBrushOld);
	DeleteObject(hBrush);
	SelectObject(hdc,hPenOld);
	DeleteObject(hPen);
	InvalidateRect(hwnd,&rcClient,0);
}
int x=450;
int width=550;
int a=0;
char s1[6]="|||||";
void catchuoi()
{
	int n=strlen(s1);
	if(n==0)
		return;
	s1[n-1]='\0';
}
void UpdateBall(RECT* prc)
{
	if(ball.x<0)
	{
		ball.dx=temp;
	}
	else if(ball.width>prc->right)
	{
		ball.dx=-temp;
	}
	if(ball.y<0)
	{
		ball.dy=temp;
	}
	else if(ball.height>prc->bottom)
	{
		ball.dy=-temp;
		a-=10;
		catchuoi();
	}
	if(ball.height>=prc->bottom-30&&ball.width>=x&&ball.x<=width)
	{
		if(ball.dy==temp&&ball.dy!=-temp)
			a+=10;
		ball.dy=-temp;
	}
	ball.x += ball.dx;
	ball.width +=ball.dx;
	ball.y+=ball.dy;
	ball.height+=ball.dy;
}

void UpdateRectRight(RECT* prc)
{
	if(width>prc->right)
		return;
	x+=value;
	width+=value;
}
void UpdateRectLeft(RECT* prc)
{
	if(x<prc->left)
		return;
	x-=value;
	width-=value;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	switch(msg)
	{
	case WM_CREATE:
		{
			HICON hIcon,hIconSm;
			HMENU hMenu,hSubMenu;

			hMenu=CreateMenu();

			hSubMenu=CreatePopupMenu();
			AppendMenu(hSubMenu,MF_STRING,ID_FILE_EXIT,"&Exit");
			AppendMenu(hMenu,MF_STRING|MF_POPUP,(UINT)hSubMenu,"&File");

			hSubMenu=CreatePopupMenu();
			AppendMenu(hSubMenu,MF_STRING,ID_HELP_ABOUT,"&Show");
			AppendMenu(hSubMenu,MF_STRING,ID_HELP_INFO,"&Hide");
			AppendMenu(hMenu,MF_STRING|MF_POPUP,(UINT)hSubMenu,"&Help");

			SetMenu(hwnd,hMenu);
			hIcon=(HICON)LoadImage(NULL,"my_icon.ico",IMAGE_ICON,32,32,LR_LOADFROMFILE);
			if(hIcon)
				SendMessage(hwnd,WM_SETICON,ICON_BIG,(LPARAM)hIcon);
			else
				MessageBox(hwnd,"Failed!","Sign hIcon",MB_OK|MB_ICONERROR);
			hIconSm=(HICON)LoadImage(NULL,"my_icon.ico",IMAGE_ICON,16,16,LR_LOADFROMFILE);
			if(hIconSm)
				SendMessage(hwnd,WM_SETICON,ICON_SMALL,(LPARAM)hIconSm);
			else
				MessageBox(hwnd,"Failed!","Sign hIcon",MB_OK|MB_ICONERROR);
			g_Result=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_RESULT),hwnd,DlgProc);
			if(g_Result)
				ShowWindow(g_Result,SW_HIDE);
			else
				MessageBox(hwnd,"Failed","Dialog",MB_OK|MB_ICONERROR);
			
    	//	hBitmap = (HBITMAP)LoadImage(NULL, "ball.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
			bool ret;
			ret=SetTimer(hwnd,ID_TIMER,1,NULL);
			if(ret == 0)
				MessageBox(hwnd, "Could not SetTimer()!", "Error", MB_OK | MB_ICONEXCLAMATION);
			ZeroMemory(&ball, sizeof(ball));
			ball.width = 30;
			ball.height = 30;
			ball.dx = temp;
			ball.dy = temp;
		}
		break;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case ID_FILE_EXIT:
			SendMessage(hwnd,WM_CLOSE,0,0);
			break;
		case ID_HELP_ABOUT:
			ShowWindow(g_Result,SW_SHOW);
			break;
		case ID_HELP_INFO:
			ShowWindow(g_Result,SW_HIDE);
			break;
		}
		break;
		
	/*case WM_PAINT:
		{
				RECT rcClient;
				PAINTSTRUCT ps;
				HDC hdc=BeginPaint(hwnd,&ps);
				GetClientRect(hwnd, &rcClient);	
				DrawText(hdc, TEXT("Text Out String"),strlen("Text Out String"), &rcClient, DT_TOP|DT_LEFT);
				TextOut(hdc, 10, 10, TEXT("Text Out String"),strlen("Text Out String"));
				DrawRectangle(hwnd,hdc,x,width);
				EndPaint(hwnd,&ps);
		}
		break;*/
	case WM_KEYDOWN:
		switch (wParam)
		{
		case VK_RIGHT:
			{
				HDC hdc = GetDC(hwnd);
				RECT rcClient;
				GetClientRect(hwnd, &rcClient);		
				UpdateRectRight(&rcClient);
				DrawRectangle(hwnd,hdc,x,width);
				ReleaseDC(hwnd,hdc);
			}
				break;
		case VK_LEFT:
			{
				HDC hdc=GetDC(hwnd);
				RECT rcClient;
				GetClientRect(hwnd,&rcClient);
				UpdateRectLeft(&rcClient);
				DrawRectangle(hwnd,hdc,x,width);
				ReleaseDC(hwnd,hdc);
			}
			break;
		}
		break;
	case WM_TIMER:
			{
				char s[100];
				HDC hdc = GetDC(hwnd);
				RECT rcClient;
				HPEN hPen,hOldPen;
				GetClientRect(hwnd, &rcClient);		
				UpdateBall(&rcClient);
				DrawBall(hwnd,hdc);
				DrawRectangle(hwnd,hdc,x,width);
				wsprintf(s,"Score: %d     %s",a,s1);
				SetTextColor(hdc,RGB(255,0,0));
				SetBkColor(hdc,RGB(0,0,0));
				DrawText(hdc, s, strlen(s), &rcClient, DT_SINGLELINE );
				ReleaseDC(hwnd,hdc);
			}	
		break;
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd,msg,wParam,lParam);
	}
	return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdLine)
{
	WNDCLASSEX wc;
	wc.cbClsExtra=0;
	wc.cbSize=sizeof(WNDCLASSEX);
	wc.cbWndExtra=0;
	wc.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
	wc.hCursor=LoadCursor(NULL,IDC_ARROW);
	wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
	wc.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance=hInstance;
	wc.lpfnWndProc=WndProc;
	wc.lpszClassName=g_szClassName;
	wc.lpszMenuName=NULL;
	wc.style=0;
	if(!RegisterClassEx(&wc))
	{
		MessageBox(NULL,"Failed!","Sign Class",MB_OK|MB_ICONEXCLAMATION);
		return 0;
	}
	HWND hwnd;
	hwnd=CreateWindowEx(WS_EX_CLIENTEDGE,g_szClassName,"Title",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,1000,500,NULL,NULL,hInstance,NULL);
	if(!hwnd)
	{
		MessageBox(NULL,"Failed!","Create Windows",MB_OK|MB_ICONEXCLAMATION);
		return 0;
	}
	ShowWindow(hwnd,nCmdLine);
	UpdateWindow(hwnd);
	MSG msg;
	while(GetMessage(&msg,NULL,0,0)>0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}